﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.IO;
using BaseDll;
namespace UserData
{
    public class ScrewData
    {
        public string strName = "";
        public int n_Index = 0;
        public double d_Torque = 0;
        public double d_Angle = 0;
        public double d_High = 0;
        public bool b_Result = true;
        public string strTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    }
    public class ScrewDataMgr
    {
        public delegate void ShowProductScrewdelegate(ScrewData data);
        public ShowProductScrewdelegate OnTestReflushScrewdelegate = null;
        private static object obj = new object();
        private static ScrewDataMgr screwTimeMgr = null;
        public static ScrewDataMgr GetInstance()
        {
            if (screwTimeMgr == null)
            {
                lock (obj)
                {
                    screwTimeMgr = new ScrewDataMgr();
                    return screwTimeMgr;
                }

            }
            else
                return screwTimeMgr;
        }

        string str_PathName = @"D:\HY System\_Machine\Config.螺丝数据.json";
        private List<ScrewData> screwDatas = new List<ScrewData>();
        public void Init()
        {
            if (!File.Exists(str_PathName))
            {
                AccessJosnSerializer.ObjectToJson(str_PathName, screwDatas);
            }
            else
            {
                object obj = AccessJosnSerializer.JsonToObject(str_PathName, typeof(List<ScrewData>));
                if (obj != null)
                {
                    screwDatas = (List<ScrewData>)obj;
                }
            }
            foreach (var item in screwDatas)
            {
                if (OnTestReflushScrewdelegate != null)
                    OnTestReflushScrewdelegate(item);
            }

        }
        public void Function_AddScrewData(ScrewData data)
        {
            screwDatas.Add(data);            
            AccessJosnSerializer.ObjectToJson(str_PathName, screwDatas);
            if (OnTestReflushScrewdelegate != null)
                OnTestReflushScrewdelegate(data);
        }

        public void Function_Clear()
        {
            screwDatas.Clear();
            AccessJosnSerializer.ObjectToJson(str_PathName, screwDatas);

        }
    }
}